package com.jeremy.myapplication

import android.graphics.PorterDuff
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val customView = findViewById<CustomView>(R.id.custom_view)
        val tvDec = findViewById<TextView>(R.id.tv_dec)
        val tvFormula =findViewById<TextView>(R.id.tv_formula)
        val modeList = mutableListOf<ModeData>()
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.SRC,
                title = "SRC",
                formula = "[Sa, Sc]",
                dec = "只显示源图像"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.SRC_ATOP,
                title = "SRC_ATOP",
                formula = "[Da, Sc * Da + (1 - Sa) * Dc]",
                dec = "在源图像和目标图像相交的地方绘制【源图像】，在不相交的地方绘制【目标图像】，相交处的效果受到源图像和目标图像alpha的影响"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.SRC_IN,
                title = "SRC_IN",
                formula = "[Sa * Da, Sc * Da]",
                dec = "只在源图像和目标图像相交的地方绘制【源图像】"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.SRC_OUT,
                title = "SRC_OUT",
                formula = "[Sa * (1 - Da), Sc * (1 - Da)]",
                dec = "只在源图像和目标图像不相交的地方绘制【源图像】，相交的地方根据目标图像的对应地方的alpha进行过滤，目标图像完全不透明则完全过滤，完全透明则不过滤"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.SRC_OVER,
                title = "SRC_OVER",
                formula = "[Sa + (1 - Sa)*Da,Sc + (1 - Sa)*Dc]",
                dec = "将源图像放在目标图像上方"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.DST,
                title = "DST",
                formula = "[Da, Dc]",
                dec = "只显示目标图像"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.DST_ATOP,
                title = "DST_ATOP",
                formula = "[Sa, Sa * Dc + Sc * (1 - Da)]",
                dec = "在源图像和目标图像相交的地方绘制【目标图像】，在不相交的地方绘制【源图像】，相交处的效果受到源图像和目标图像alpha的影响"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.DST_IN,
                title = "DST_IN",
                formula = "[Da * Sa,Dc * Sa]",
                dec = "只在源图像和目标图像相交的地方绘制【目标图像】，绘制效果受到源图像对应地方透明度影响"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.DST_OUT,
                title = "DST_OUT",
                formula = "[Da * (1 - Sa), Dc * (1 - Sa)]",
                dec = "只在源图像和目标图像不相交的地方绘制【目标图像】，在相交的地方根据源图像的alpha进行过滤，源图像完全不透明则完全过滤，完全透明则不过滤"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.DST_OVER,
                title = "DST_OVER",
                formula = "[Sa + (1 - Sa)*Da,Dc + (1 - Da)*Sc]",
                dec = "将目标图像放在源图像上方"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.DARKEN,
                title = "DARKEN",
                formula = "[Sa + Da - Sa*Da,Sc*(1 - Da) + Dc*(1 - Sa) + max(Sc, Dc)]",
                dec = "变暗,较深的颜色覆盖较浅的颜色，若两者深浅程度相同则混合"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.LIGHTEN,
                title = "LIGHTEN",
                formula = "[Sa + Da - Sa*Da,Sc*(1 - Da) + Dc*(1 - Sa) + max(Sc, Dc)]",
                dec = "变亮，与DARKEN相反，DARKEN和LIGHTEN生成的图像结果与Android对颜色值深浅的定义有关"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.OVERLAY,
                title = "OVERLAY",
                formula = "暂无公式",
                dec = "叠加"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.SCREEN,
                title = "SCREEN",
                formula = "[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]",
                dec = "滤色，色调均和,保留两个图层中较白的部分，较暗的部分被遮盖"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.MULTIPLY,
                title = "MULTIPLY",
                formula = "[Sa * Da, Sc * Dc]",
                dec = "正片叠底，源图像素颜色值乘以目标图像素颜色值除以255得到混合后图像像素颜色值"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.XOR,
                title = "XOR",
                formula = "[Sa + Da - Sa*Da,Sc*(1 - Da) + Dc*(1 - Sa) + min(Sc, Dc)]",
                dec = "在源图像和目标图像相交的地方之外绘制它们，在相交的地方受到对应alpha和色值影响，如果完全不透明则相交处完全不绘制。"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.CLEAR,
                title = "CLEAR",
                formula = "[0, 0]",
                dec = "清除图像"
            )
        )
        modeList.add(
            ModeData(
                mode = PorterDuff.Mode.ADD,
                title = "ADD",
                formula = "Saturate(S + D)",
                dec = "SRC与DST两张图片相交区域的饱和度进行相加"
            )
        )

        val modeAdapter = ModeAdapter(modeList) { mode: ModeData ->
            customView.setMode(mode.mode)
            tvDec.text = mode.dec
            tvFormula.text ="公式为  :${mode.formula}"
        }
        val rv = findViewById<RecyclerView>(R.id.rv_mode)
        rv.layoutManager = GridLayoutManager(this, 3)
        rv.adapter = modeAdapter


        val rgView = findViewById<RadioGroup>(R.id.rg_view)
        val rgType = findViewById<RadioGroup>(R.id.rg_type)
        rgView.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_dst -> {
                    customView.showDst()
                }
                R.id.rb_src -> {
                    customView.showSrc()
                }
                R.id.rb_demo -> {
                    customView.showDemo()
                }
            }
        }
        rgType.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_google -> {
                    customView.setIsGoogle(true)
                }
                R.id.rb_self -> {
                    customView.setIsGoogle(false)
                }
            }
        }
    }
}